package cn.rxmind.dlicense.core.validator;

import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.utils.MachineInfoUtil;
import lombok.Setter;

import java.util.Set;

/**
 * IP 地址校验器
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/15 2:58 下午
 */
public class IPAddressLicenseValidator extends AbstractExitJVMLicenseValidator {

    @Override
    public String description() {
        return "IP 地址校验";
    }

    @Override
    protected ValidateResult validateResult(DLicenseContent content) {

        IpAddressValidateResult result = new IpAddressValidateResult();
        if(content.getIpRule() != null){
            Set<String> ipAddress = MachineInfoUtil.getIpAddress();
            result.setValid(content.getIpRule().test(ipAddress));
        }

        return result;
    }

    @Setter
    static class IpAddressValidateResult implements ValidateResult{

        private boolean valid;

        @Override
        public Class<IPAddressLicenseValidator> getLicenseValidator() {
            return IPAddressLicenseValidator.class;
        }

        @Override
        public boolean valid() {
            return this.valid;
        }

        @Override
        public String getDescription() {

            return valid() ? "valid" : "IP地址不匹配";
        }
    }
}
